<html lang="en">
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <head th:insert="~{import :: head}"></head>
    <meta charset="UTF-8">
    <title>编辑动态文章</title>
    <script type="text/javascript">
        $(function () {
            tabNav("动态");
        })

        function clickSaveTrends() {

            let param = {}
            const title = $("#title").val().trim();
            if (title.length===0) {
                ShowFailure("标题不能为空!");
                return;
            }
            param["title"] = title;

            const contentURL = $("#contentURL").val().trim();
            const content = $("#content").val().trim();

            const source = $("#source").val();
            if (source==0) {
                if (contentURL.length===0) {
                    ShowFailure("公众号文章 URL 不能为空!");
                    return;
                }
                param["contentURL"] = contentURL;
            } else {
                if (content.length===0) {
                    ShowFailure("文章内容不能为空!");
                    return;
                }
                param["content"] = content;
            }

            // 排序只能为整数，并且小于
            let queue = $("#queue").val().trim();
            if (queue.length==0) {
                ShowFailure("排序不能为空!");
                return;
            } else {
                let r = /^-?\d{1,6}$/;
                if(!r.test(queue)){
                    ShowFailure("请输入整数!");
                    return;
                }
                param['queue'] = queue;
            }

            const coverURL = $("#coverURL").val().trim();
            if (coverURL.length!==0) {
                param["coverURL"] = coverURL;
            }
            param["id"] = $("#newsId").val();

            $.ajax({
                // url: "/oa/trends/save",
                url: "[[@{/trends/save}]]",
                type: "post",
                data: param,
                dataType: "text",
                success: function(resp) {
                    // console.info(resp);
                    if (resp==="success") {
                        ShowSuccess("添加成功!");
                        window.location.href = "[[@{/trends}]]";
                    } else {
                        alert("添加失败, 请联系开发人员!");
                    }
                }
            })
        }

        function sourceChange(obj) {
            if (obj.value==0) {
                $(".gzh-add").show();
                $(".self-add").hide();
            } else {
                $(".gzh-add").hide();
                $(".self-add").show();
            }
        }
    </script>
</head>
<body>
<div th:insert="~{navigation :: nav}"></div>

<div style="width: 700px; margin: 30px auto;">
    <input id="newsId" type="text" style="display: none;" th:value="${newsInfo.id}">
    <div class="form-group">
        <label for="title">标题</label>
        <input type="text" class="form-control" id="title" placeholder="请输入标题"
               th:value="${newsInfo.title}">
    </div>
    <div class="form-group">
        <label>文章来源</label>
        <select class="form-control" onchange="sourceChange(this)" id="source">
            <option value="0" th:selected="${source} eq 0">公众号文章</option>
            <option value="1" th:selected="${source} eq 1">自主创建</option>
        </select>
    </div>
    <div class="form-group gzh-add" th:style="'display:'+ (${source==0? '': 'none'})">
        <label for="contentURL">公众号文章 URL</label>
        <input type="text" class="form-control" id="contentURL" placeholder="公众号文章 url"
               th:value="${newsInfo.contentUrl}">
    </div>
    <div class="form-group gzh-add" th:style="'display:'+ (${source==0? '': 'none'})">
        <label for="coverURL">公众号文章封面图 URL</label>
        <input type="text" class="form-control" id="coverURL" placeholder="文章封面图 url"
               th:value="${newsInfo.coverUrl}">
    </div>
    <div class="form-group self-add" th:style="'display:'+ (${source==1? '': 'none'})">
        <label for="content">内容</label>
        <textarea class="form-control" rows="22" id="content" placeholder="请输入..."
                  th:text="${newsInfo.content}"></textarea>
    </div>
    <div class="form-group">
        <label for="queue">排序（用于文章列表排序，值越大越靠前，默认值100）</label>
        <input type="text" class="form-control" id="queue" maxlength="6" placeholder="请输入整数"
               th:value="${newsInfo.queue}">
    </div>
    <br>
    <a type="button" class="btn btn-default" href="javascript:history.go(-1);">取消</a>
    &nbsp;&nbsp;
    <button type="button" class="btn btn-primary" onclick="clickSaveTrends()">保存</button>
</div>

<script th:src="@{/js/custom.js}"></script>
</body>
</html>